﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

using Base;
using Base.DataModel;

using XmlCompiler;

namespace Test
{
    class Program
    {
        static Entity DefineProduct()
        {
            // Create the product entity kind
            Entity product = new Entity("Product") { PluralName = "Products" };

            // Fields
            product.Fields.Add(new Field("Title", new Fieldlet("Title")));
            product.Fields.Add(new Field("List Price", new Fieldlet("List Price")));
            product.Fields.Add(new Field("Type", new Fieldlet("Type")));
            product.Fields.Add(new Field("Publication Date", new Fieldlet("Publication Date", FieldletType.Date)));
            product.Fields.Add(new Field("Sales Rank", new Fieldlet("Sales Rank", FieldletType.Int)));
            product.Fields.Add(new Field("ASIN", new Fieldlet("ASIN")));
            product.Fields.Add(new MultiField("Subjects", new Fieldlet("Subject")));
            product.Fields.Add(new MultiField("Attributes", new Fieldlet("Type"), new Fieldlet("Value")));
            product.Fields.Add(new MultiField("Reviews",
                new Fieldlet("Source"), new Fieldlet("Review", FieldletType.Text)));

            // Related products
            product.Fields.Add(new MultiField("Related Products", new Fieldlet("Product", FieldletType.Link)));

            // Publisher
            product.Fields.Add(new MultiField("Publishers", new Fieldlet("Company", FieldletType.Link)));

            // Contributors
            product.Fields.Add(new MultiField("Contributors",
                new Fieldlet("Contributor", FieldletType.Link),
                new Fieldlet("Role")));

            return product;
        }

        static Entity DefineCompany()
        {
            // Create the company entity kind
            Entity company = new Entity("Company") { PluralName = "Companies" };

            // Fields
            company.Fields.Add(Field.SingleFieldlet(new Fieldlet("Name")));
            company.Fields.Add(new MultiField("Products", new Fieldlet("Product", FieldletType.Link)));

            return company;
        }

        static Entity DefinePerson()
        {
            // Create the Person entity kind
            Entity person = new Entity("Person") { PluralName = "People" };

            // Fields
            person.Fields.Add(new Field("Name", new Fieldlet("First"), new Fieldlet("Last")));
            person.Fields.Add(new MultiField("Contributions", new Fieldlet("Products", FieldletType.Link)));

            return person;
        }

        static void SetLinks(Entity person, Entity company, Entity product)
        {
            // Product.Contributors <=> Person.Contributions
            product.Fields["Contributors"].DuplicateToField(person, person.Fields["Contributions"]);

            // Product.Publisher <=> Company.Products
            product.Fields["Publishers"].DuplicateToField(company, company.Fields["Products"]);

            // Product.Related <=> Product.Related
            Field rel = product.Fields["Related Products"];
            rel.DuplicateToField(product, rel);
        }

        static void Main(string[] args)
        {
            NiftySchema s = new NiftySchema("Amazonifty");

            s.Entities.Add(DefinePerson());
            s.Entities.Add(DefineCompany());
            s.Entities.Add(DefineProduct());

            SetLinks(s.Entities["Person"], s.Entities["Company"], s.Entities["Product"]);

            // Compile the schema
            SchemaCompiler.Compile(s, File.Open("C:\\temp.txt", FileMode.Create));
        }
    }
}
